package com.example.memorypoint.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.util.Log;

import com.example.memorypoint.AAChartCoreLib.AAChartCreator.AAChartModel;
import com.example.memorypoint.AAChartCoreLib.AAChartCreator.AAChartView;
import com.example.memorypoint.AAChartCoreLib.AAChartCreator.AASeriesElement;
import com.example.memorypoint.AAChartCoreLib.AAChartEnum.AAChartAnimationType;
import com.example.memorypoint.AAChartCoreLib.AAChartEnum.AAChartStackingType;
import com.example.memorypoint.AAChartCoreLib.AAChartEnum.AAChartType;
import com.example.memorypoint.AAChartCoreLib.AAOptionsModel.AAOptions;
import com.example.memorypoint.AAChartCoreLib.AAOptionsModel.AAPlotBandsElement;
import com.example.memorypoint.AAChartCoreLib.AAOptionsModel.AAYAxis;
import com.example.memorypoint.AAChartCoreLib.AATools.AAColor;
import com.example.memorypoint.DateBase.DiaryDatabase;
import com.example.memorypoint.R;
import com.example.memorypoint.entity.Diary;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ChartActivity extends AppCompatActivity {

    private AAChartView chartFirst;
    private AAChartView chartSecond;
    private Toolbar toolbar;
    private DiaryDatabase database;
    private Object[] contentLength;
    private Object[] countDev;
    private List<Diary> diaryList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart);
        toolbar = findViewById(R.id.toolbar);
        chartFirst = findViewById(R.id.chart_one);
        chartSecond = findViewById(R.id.chart_two);
        database = new DiaryDatabase(getBaseContext());
        initFirstData();
        initSecondData();
        chartOneData();
        chartTwoData();
        toolbarBack();

    }

    private void initFirstData() {
        String s1 = "00:00:00";
        String s2 = "06:00:00";
        String s3 = "12:00:00";
        String s4 = "18:00:00";
        String s5 = "24:00:00";

        countDev = new Object[4];
        int[] count = new int[4];

        diaryList = database.getDairyList();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime time1 = LocalTime.parse(s1, timeFormatter);
        LocalTime time2 = LocalTime.parse(s2, timeFormatter);
        LocalTime time3 = LocalTime.parse(s3, timeFormatter);
        LocalTime time4 = LocalTime.parse(s4, timeFormatter);
        LocalTime time5 = LocalTime.parse(s5, timeFormatter);

        for (int i = 0; i < diaryList.size(); i++) {
            LocalTime time = LocalTime.parse(diaryList.get(i).getTime(), timeFormatter);
            if (time.isAfter(time1) && time.isBefore(time2)) {
                count[0]++;
            }
            if (time.isAfter(time2) && time.isBefore(time3)) {
                count[1]++;
            }
            if (time.isAfter(time3) && time.isBefore(time4)) {
                count[2]++;
            }
            if (time.isAfter(time4) && time.isBefore(time5)) {
                count[3]++;
            }
        }
        for (int i = 0; i < count.length; i++) {
            countDev[i] = count[i];
        }
    }

    private void initSecondData() {
        contentLength = new Object[diaryList.size()];
        for (int i = 0; i < diaryList.size(); i++) {
            contentLength[i] = diaryList.get(i).getContent().length() + diaryList.get(i).getTitle().length();
        }
    }

    private void chartOneData() {

        AASeriesElement element1 = new AASeriesElement()
                .name("数量")
                .data(countDev);

        AAChartModel aaChartModel = new AAChartModel()
                .dataLabelsEnabled(false)
                .yAxisGridLineWidth(0)
                .touchEventEnabled(true)
                .chartType(AAChartType.Column)
                .series(new AASeriesElement[]{element1})
                .categories(new String[]{
                        "6:00-12:00", "12:00-18:00", "18:00-24:00", "00:00-6:00"})
                .legendEnabled(true)
                .colorsTheme(new String[]{"#fe117c", "#ffc069", "#06caf4", "#7dffc0"})
                .animationType(AAChartAnimationType.EaseOutCubic)
                .animationDuration(1200)
                .stacking(AAChartStackingType.Normal)
                .borderRadius(10f);
        AAOptions options = aaChartModel.aa_toAAOptions();
        chartFirst.aa_drawChartWithChartOptions(options);
    }

    private void chartTwoData() {
        AAChartModel aaChartModel = new AAChartModel()
                .chartType(AAChartType.Spline)//图形类型
                .dataLabelsEnabled(false)
                .markerRadius(0)
                .yAxisMax(40)
                .series(new AASeriesElement[]{
                        new AASeriesElement()
                                .name("字词数量")
                                .data(contentLength)
                                .color(AAColor.White)
                                .lineWidth(10),
                });

        AAOptions aaOptions = aaChartModel.aa_toAAOptions();
        AAPlotBandsElement[] aaPlotBandsElementArr = {
                new AAPlotBandsElement()
                        .from(0)
                        .to(5)
                        .color("#BC2B44")
                ,
                new AAPlotBandsElement()
                        .from(5)
                        .to(10)
                        .color("#EC6444")
                ,
                new AAPlotBandsElement()
                        .from(10)
                        .to(15)
                        .color("#f19742")
                ,
                new AAPlotBandsElement()
                        .from(15)
                        .to(20)
                        .color("#f3da60")
                ,
                new AAPlotBandsElement()
                        .from(20)
                        .to(25)
                        .color("#9bd040")
                ,
                new AAPlotBandsElement()
                        .from(25)
                        .to(50)
                        .color("#acf08f")
                ,
        };

        AAYAxis aaYAxis = aaOptions.yAxis;
        aaYAxis.plotBands(aaPlotBandsElementArr);
        chartSecond.aa_drawChartWithChartOptions(aaOptions);
    }

    private void toolbarBack() {
        toolbar.setNavigationOnClickListener(view ->
                finish());
    }
}